﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data;

namespace HQWeb.LibarySys
{
    /// <summary>
    /// LibraryServer 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    [GenerateScriptType(typeof(Model.LibrarySys.T_PUB_LIBRARYOUT))]
    [GenerateScriptType(typeof(Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL))]
    [GenerateScriptType(typeof(Model.SysManage.T_PUB_LIBRARY))]
    [GenerateScriptType(typeof(Model.LibrarySys.T_PUB_SCRAP))]
    [GenerateScriptType(typeof(Model.Procurement.T_PUB_PURCHASEMATERIAL))]
    [GenerateScriptType(typeof(Model.Procurement.T_PUB_WAREHOUSING))]
    public class LibraryServer : System.Web.Services.WebService
    {
        #region 添加出库单材料
        [WebMethod(Description = "添加出库单材料",EnableSession=true)]
        public string Add(Model.LibrarySys.T_PUB_LIBRARYOUT libraryout, IList<Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL> materiallist)
        {
            string reout = "";
            if (Session["USER_ID"] != null)
            {
                string strGIud = System.Guid.NewGuid().ToString();
                libraryout.ID = strGIud;
                libraryout.APPLY_TIME = DateTime.Now;
                libraryout.APPLICANT = Convert.ToInt32(Session["USER_ID"]);
                libraryout.APPLICANT_NAME = Convert.ToString(Session["USER_NAME"]);
                foreach (Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL item in materiallist)
                {
                    item.LIBRARYOUTID = strGIud;
                    item.MATERIAL_STATE = "0";
                }
                BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL bll = new BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL();
                try
                {
                    bll.AddMaterials(libraryout, materiallist);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(libraryout.APPLICANT), "添加" + libraryout.TITEL + "--出库申请", DateTime.Now);
                }
                catch (Exception ex)
                {
                    reout = ex.Message;
                }
            }
            else
            {
                reout = "SessionTimeout";
            }
            return reout;
        }
        #endregion 编辑出库单材料

        #region 采购入库材料
        [WebMethod(Description = "添加出库单材料",EnableSession=true)]
        public string AddWarehousing(Model.Procurement.T_PUB_WAREHOUSING warehousing, IList<Model.Procurement.T_PUB_PURCHASEMATERIAL> materiallist)
        {
            string reout = "";
            if (Session["USER_ID"] != null)
            {
                int materialid;
                warehousing.APPLICANT = Convert.ToInt32(Session["USER_ID"]);
                warehousing.APPLICANT_NAME = Convert.ToString(Session["USER_NAME"]);
                int libraryid = (int)warehousing.LIBRARY_ID;
                BLL.LibrarySys.T_PUB_LIBRARYMATERIAL bll = new BLL.LibrarySys.T_PUB_LIBRARYMATERIAL();
                IList<Model.LibrarySys.T_PUB_LIBRARYMATERIAL> addList = new List<Model.LibrarySys.T_PUB_LIBRARYMATERIAL>();
                IList<Model.LibrarySys.T_PUB_LIBRARYMATERIAL> editList = new List<Model.LibrarySys.T_PUB_LIBRARYMATERIAL>();
                Model.LibrarySys.T_PUB_LIBRARYMATERIAL model;
                string strGIud = System.Guid.NewGuid().ToString();
                warehousing.WAREHOUSING_ID = strGIud;
                warehousing.TIME = DateTime.Now;
                foreach (Model.Procurement.T_PUB_PURCHASEMATERIAL item in materiallist)
                {
                    item.PURCHASE_ID = strGIud;
                    materialid = (int)item.MATERIAL_ID;
                    model = bll.GetModel(libraryid, materialid);
                    if (model != null)
                    {
                        model.NUM += item.NUM;
                        editList.Add(model);
                    }
                    else
                    {
                        model = new Model.LibrarySys.T_PUB_LIBRARYMATERIAL();
                        model.LIBRARY_ID = libraryid;
                        model.MATERIAL_ID = item.MATERIAL_ID;
                        model.NUM = item.NUM;
                        addList.Add(model);
                    }
                }

                try
                {
                    bll.AddPurchase(warehousing, materiallist, addList, editList);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(warehousing.APPLICANT), "添加" + warehousing.TITEL + "--入库单", DateTime.Now);
                }
                catch (Exception ex)
                {
                    reout = ex.Message;
                }
            }
            else
            {
                reout = "SessionTimeout";
            }
            return reout;
        }
        #endregion 编辑出库单材料

        #region 更新出库单材料
        [WebMethod(Description = "更新出库单材料", EnableSession = true)]
        public string Edit(Model.LibrarySys.T_PUB_LIBRARYOUT libraryout, IList<Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL> materiallist)
        {
            string reout = "";
            if (Session["USER_ID"] != null)
            {
                libraryout.APPLY_TIME = DateTime.Now;
                libraryout.APPLICANT = Convert.ToInt32(Session["USER_ID"]);
                libraryout.APPLICANT_NAME = Convert.ToString(Session["USER_NAME"]);
                BLL.LibrarySys.T_PUB_LIBRARYOUT lbll = new BLL.LibrarySys.T_PUB_LIBRARYOUT();
                Model.LibrarySys.T_PUB_LIBRARYOUT premodel = lbll.GetModel(libraryout.ID);
                libraryout.NOTATION = premodel.NOTATION;
                libraryout.APPROVER = premodel.APPROVER;
                libraryout.APPROVER_NAME = premodel.APPROVER_NAME;
                IList<Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL> addList = new List<Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL>();
                IList<Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL> editList = new List<Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL>();
                foreach (Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL item in materiallist)
                {
                    item.MATERIAL_STATE = "0";
                    if (item.ID == 0)
                    {
                        addList.Add(item);
                    }
                    else
                    {
                        editList.Add(item);
                    }
                }
                BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL bll = new BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL();
                try
                {
                    bll.EditMaterials(libraryout, addList, editList);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(libraryout.APPLICANT), "更新" + libraryout.TITEL + "--出库申请", DateTime.Now);
                }
                catch (Exception ex)
                {
                    reout = ex.Message;
                }
            }
            else
            {
                reout = "SessionTimeout";
            }
            return reout;
        }
        #endregion

        #region 绑定分店仓库
        [WebMethod]
        public IList<Model.SysManage.T_PUB_LIBRARY> BindLibrary(int Branch_id)
        {
            BLL.SysManage.T_PUB_LIBRARY bll = new BLL.SysManage.T_PUB_LIBRARY();
            List<Model.SysManage.T_PUB_LIBRARY> modellist = new List<Model.SysManage.T_PUB_LIBRARY>();
            modellist = bll.GetModelByBranch_id(Branch_id);
            return modellist;
        }
        #endregion

        #region 删除出库单材料
        [WebMethod(EnableSession=true)]
        public string DelLibraryMaterials(string strID)
        {
            string strResout = "";
            if (Session["USER_ID"] != null)
            {
                BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL bll = new BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL();
                int temp;
                int.TryParse(strID, out temp);
                try
                {
                    bll.Delete(temp);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "删除" + strID + "--出库单材料", DateTime.Now);
                }
                catch (Exception ex)
                {
                    strResout = ex.Message;
                }
            }
            else
            {
                strResout = "SessionTimeout";
            }
            return strResout;
        }
        #endregion

        #region 入库数量少于出库时数量填写报销单
        [WebMethod(Description = "入库数量少于出库时数量填写报销单", EnableSession=true)]
        public int AddScrap(Model.LibrarySys.T_PUB_SCRAP model)
        {
            int strResout = 0 ;
            if (Session["USER_ID"] != null)
            {
                BLL.LibrarySys.T_PUB_SCRAP bll = new BLL.LibrarySys.T_PUB_SCRAP();
                model.APPLY_TIME = DateTime.Now;
                model.APPROVE_TIME = model.APPLY_TIME;
                model.APPLICANT = Convert.ToInt32(Session["USER_ID"]);
                model.APPLICANT_NAME = Convert.ToString(Session["USER_NAME"]);
                try
                {
                    strResout = bll.Add(model);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(model.APPLICANT), "添加" + model.TITEL + "--报销单", DateTime.Now);
                }
                catch (Exception ex)
                {
                    strResout = -1;
                }
            }
            else
            {
                strResout = -2;
            }
            return strResout;
        }
        #endregion

        #region 添加采购单
        [WebMethod(Description = "添加采购单",EnableSession=true)]
        public string AddPurchase(Model.Procurement.T_PUB_PURCHASE model, IList<Model.Procurement.T_PUB_PURCHASEMATERIAL> addList)
        {
            string reout = "";
            if (Session["USER_ID"] != null)
            {
                string strGIud = System.Guid.NewGuid().ToString();
                model.ID = strGIud;
                model.STATE = "1";
                model.APPLICANT_TIME = DateTime.Now;
                model.APPLICANT = Convert.ToInt32(Session["USER_ID"]);
                model.APPLICANT_NAME = Convert.ToString(Session["USER_NAME"]);
                foreach (Model.Procurement.T_PUB_PURCHASEMATERIAL item in addList)
                {
                    item.PURCHASE_ID = strGIud;
                }
                BLL.Procurement.T_PUB_PURCHASEMATERIAL bll = new BLL.Procurement.T_PUB_PURCHASEMATERIAL();
                try
                {
                    bll.AddPurchase(model, addList);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(model.APPLICANT), "添加" + model.TITLE + "--采购单", DateTime.Now);
                }
                catch (Exception ex)
                {
                    reout = ex.Message;
                }
            }
            else
            {
                reout = "SessionTimeout";
            }
            return reout;
        }
        #endregion

        #region 编辑采购单
        [WebMethod(Description = "编辑采购单", EnableSession = true)]
        public string EditPurchase(Model.Procurement.T_PUB_PURCHASE model, IList<Model.Procurement.T_PUB_PURCHASEMATERIAL> AllList)
        {
            string reout = "";
            if (Session["USER_ID"] != null)
            {
                IList<Model.Procurement.T_PUB_PURCHASEMATERIAL> addList = new List<Model.Procurement.T_PUB_PURCHASEMATERIAL>();
                IList<Model.Procurement.T_PUB_PURCHASEMATERIAL> editList = new List<Model.Procurement.T_PUB_PURCHASEMATERIAL>();
                BLL.Procurement.T_PUB_PURCHASEMATERIAL bll = new BLL.Procurement.T_PUB_PURCHASEMATERIAL();
                BLL.Procurement.T_PUB_PURCHASE pbll = new BLL.Procurement.T_PUB_PURCHASE();
                Model.Procurement.T_PUB_PURCHASE premodel = pbll.GetModel(model.ID);
                model.APPLICANT_TIME = DateTime.Now;
                model.STATE = "1";
                model.NOTATION = premodel.NOTATION;
                model.APPROVER = premodel.APPROVER;
                model.APPROVER_NAME = premodel.APPROVER_NAME;
                model.APPLICANT = Convert.ToInt32(Session["USER_ID"]);
                model.APPLICANT_NAME = Convert.ToString(Session["USER_NAME"]);
                foreach (Model.Procurement.T_PUB_PURCHASEMATERIAL item in AllList)
                {
                    if (item.ID == 0)
                    {
                        addList.Add(item);
                    }
                    else
                    {
                        editList.Add(item);
                    }
                }
                try
                {
                    bll.EditPurchase(model, addList, editList);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(model.APPLICANT), "修改" + model.TITLE + "--采购单", DateTime.Now);
                }
                catch (Exception ex)
                {
                    reout = ex.Message;
                }
            }
            else
            {
                reout = "SessionTimeout";
            }
            return reout;
        }
        #endregion

        #region 删除采购单材料
        [WebMethod(EnableSession=true)]
        public string DelPurchaseMaterials(string strID)
        {
            string strResout = "";
            if (Session["USER_ID"] != null)
            {
                BLL.Procurement.T_PUB_PURCHASEMATERIAL bll = new BLL.Procurement.T_PUB_PURCHASEMATERIAL();
                int temp;
                int.TryParse(strID, out temp);
                try
                {
                    bll.Delete(temp);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "删除" + strID + "--采购单材料", DateTime.Now);
                }
                catch (Exception ex)
                {

                    strResout = ex.Message;
                }
            }
            else
            {
                strResout = "SessionTimeout";
            }
            return strResout;
        }
        #endregion

        #region 获得出库申请出库主题
        [WebMethod(Description = "智能获取出库申请主题", EnableSession = true)]
        public string[] GetApplyTitel(string prefixText, int count)
        {
            string s;
            DataTable dt = new DataTable();
            BLL.LibrarySys.T_PUB_LIBRARYOUT bll = new BLL.LibrarySys.T_PUB_LIBRARYOUT();
            List<string> list = new List<string>();
            if (Session["BRANCH_ID"] != null)
            {
                if (Convert.ToBoolean(Session["IsAll"]) == false)
                {
                    int branch_id = Convert.ToInt32(Session["BRANCH_ID"]);
                    dt = bll.GetApplyTitel(branch_id).Tables[0];
                }
                else
                {
                    dt = bll.GetApplyTitel().Tables[0];
                }
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                s = dt.Rows[i][0].ToString();
                if (s.StartsWith(prefixText))
                {
                    list.Add(s);
                }
            }
            return list.ToArray();
        }
        #endregion

        #region 获取报修申请主题
         [WebMethod(Description = "获取报修申请主题", EnableSession = true)]
        public string[] GetRepairApplyTitel(string prefixText, int count)
        {
            string s;
            DataTable dt = new DataTable();
            BLL.LibrarySys.T_PUB_SCRAP bll = new BLL.LibrarySys.T_PUB_SCRAP();
            List<string> list = new List<string>();
            if (Session["BRANCH_ID"] != null)
            {
                if (Convert.ToBoolean(Session["IsAll"]) == false)
                {
                    int branch_id = Convert.ToInt32(Session["BRANCH_ID"]);
                    dt = bll.GetRepairApplyTitel(branch_id).Tables[0];
                }
                else
                {
                    dt = bll.GetRepairApplyTitel().Tables[0];
                }
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                s = dt.Rows[i][0].ToString();
                if (s.StartsWith(prefixText))
                {
                    list.Add(s);
                }
            }
            return list.ToArray();
        }
        #endregion

        #region 获取报废申请主题
         [WebMethod(Description = "获取报废申请主题", EnableSession = true)]
        public string[] GetScrapApplyTitel(string prefixText, int count)
        {
            string s;
            DataTable dt = new DataTable();
            BLL.LibrarySys.T_PUB_SCRAP bll = new BLL.LibrarySys.T_PUB_SCRAP();
            List<string> list = new List<string>();
            if (Session["BRANCH_ID"] != null)
            {
                if (Convert.ToBoolean(Session["IsAll"]) == false)
                {
                    int branch_id = Convert.ToInt32(Session["BRANCH_ID"]);
                    dt = bll.GetScrapApplyTitel(branch_id).Tables[0];
                }
                else
                {
                    dt = bll.GetScrapApplyTitel().Tables[0];
                }
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                s = dt.Rows[i][0].ToString();
                if (s.StartsWith(prefixText))
                {
                    list.Add(s);
                }
            }
            return list.ToArray();
        }
        #endregion

        #region 获取采购申请主题
        [WebMethod(Description = "获取采购申请主题", EnableSession = true)]
        public string[] GetPurchaseApplyTitel(string prefixText, int count)
        {
            string s;
            DataTable dt = new DataTable();
            BLL.Procurement.T_PUB_PURCHASE bll = new BLL.Procurement.T_PUB_PURCHASE();
            List<string> list = new List<string>();
            if (Session["BRANCH_ID"] != null)
            {
                if (Convert.ToBoolean(Session["IsAll"]) == false)
                {
                    int branch_id = Convert.ToInt32(Session["BRANCH_ID"]);
                    dt = bll.GetPurchaseApplyTitel(branch_id).Tables[0];
                }
                else
                {
                    dt = bll.GetPurchaseApplyTitel().Tables[0];
                }
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                s = dt.Rows[i][0].ToString();
                if (s.StartsWith(prefixText))
                {
                    list.Add(s);
                }
            }
            return list.ToArray();
        }
        #endregion

        #region 获取采购入库主题
        [WebMethod(Description = "获取采购入库主题", EnableSession = true)]
        public string[] GetPurchaseInApplyTitel(string prefixText, int count)
        {
            string s;
            DataTable dt = new DataTable();
            BLL.Procurement.T_PUB_WAREHOUSING bll = new BLL.Procurement.T_PUB_WAREHOUSING();
            List<string> list = new List<string>();
            if (Session["BRANCH_ID"] != null)
            {
                if (Convert.ToBoolean(Session["IsAll"]) == false)
                {
                    int branch_id = Convert.ToInt32(Session["BRANCH_ID"]);
                    dt = bll.GetPurchaseInApplyTitel(branch_id).Tables[0];
                }
                else
                {
                    dt = bll.GetPurchaseInApplyTitel().Tables[0];
                }
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                s = dt.Rows[i][0].ToString();
                if (s.StartsWith(prefixText))
                {
                    list.Add(s);
                }
            }
            return list.ToArray();
        }
        #endregion

    }
}
